package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.RbacPerm;
import com.woniu.entity.ResponseEntity;
import com.woniu.mapper.RbacPermMapper;
import com.woniu.service.RbacPermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author Rain
 * @Description :
 * @date 2022/09/03 10:25
 */
@Service
public class RbacPermServiceImpl implements RbacPermService {

    @Autowired
    RbacPermMapper mapper;

    @Override
    public ResponseEntity findAll(int pageNum, int pageSize) {
        return new ResponseEntity(200, "OK", null);
    }

    @Override
    public ResponseEntity findOne(int id) {
        return null;
    }

    @Override
    public ResponseEntity addOne(RbacPerm perm) {
        int count = mapper.findCountByName(perm.getName());
        if (count > 0) {
            return new ResponseEntity(500, "新增失败,权限名重复", null);
        } else {
            int i = mapper.insertOne(perm);
            if (i > 0) {
                return new ResponseEntity(200, "新增成功", null);
            }
            return new ResponseEntity(500, "新增失败", null);
        }
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int i = mapper.deleteOne(id);
        if (i > 0) {
            return new ResponseEntity(200, "删除成功", null);
        }
        return new ResponseEntity(500, "删除失败", null);
    }

    @Override
    public ResponseEntity updateOne(RbacPerm perm) {
        int count = mapper.findCountByNameOrIdOrCode(perm);
        if (count > 1) {
            return new ResponseEntity(500, "修改失败,权限名或CODE重复", null);
        } else {
            int i = mapper.updateOne(perm);
            if (i > 0) {
                return new ResponseEntity(200, "修改成功", null);
            }
            return new ResponseEntity(500, "修改失败", null);

        }
    }

    @Override
    public ResponseEntity findByCondition(int pageNum, int pageSize, RbacPerm searchObj) {
        PageHelper.startPage(pageNum, pageSize);
        List<RbacPerm> con = mapper.findByCondition(searchObj);
        PageInfo<RbacPerm> info = new PageInfo<>(con);
        return new ResponseEntity(200, "查询成功", info);
    }

    @Override
    public Set<String> findPermBySetRoles(String username) {
        return mapper.findPermBySetRoles(username);
    }

    @Override
    public ResponseEntity updatePermStatus(Integer id, String status) {
        int i = mapper.updatePermStatus(id,status);
        if (i > 0) {
            return new ResponseEntity(200, "修改状态成功", null);
        }
        return new ResponseEntity(500, "修改状态失败", null);

    }
}
